package com.hue.gulimail.gulimailorder.interceptor;

import com.hue.common.constant.AuthConstant;
import com.hue.common.vo.UserVo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 姚凤桥
 * @Date: 2021/12/24 22:58
 * @Description:
 */
public class LoginStatusInterceptor implements HandlerInterceptor {
    public static ThreadLocal<UserVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        AntPathMatcher matcher = new AntPathMatcher();

        //将mq的查询订单信息的请求放行
        boolean match1 = matcher.match("/gulimailorder/order/getBySn/**", uri);
        //将支付成功后的异步回调放行
        boolean match2 = matcher.match("/gulimailorder/order/paySuccess", uri);
        if (match1 || match2) {
            return true;
        }

        UserVo loginUser = (UserVo) request.getSession().getAttribute(AuthConstant.LOGIN_USER);

        //没有登录就跳转到登录页面
        if (loginUser == null) {
            response.sendRedirect("http://auth.gulimail.com");
            return false;
        }

        threadLocal.set(loginUser);

        return true;
    }
}
